package com.wwy.monitor.config;

import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.transaction.annotation.TransactionManagementConfigurer;

import javax.sql.DataSource;
import java.util.*;

@Configuration
@EnableTransactionManagement
public class MyBatisConfig implements TransactionManagementConfigurer {

    static Set<String> packages = new HashSet<String>();

    static {
        packages.add("com.autonavi.acis.system.model.dto");
        packages.add("com.autonavi.acis.system.model.entity");
        packages.add("com.autonavi.acis.resource.model.dto");
        packages.add("com.autonavi.acis.product.entity");
        packages.add("com.autonavi.acis.mc.entity");
        packages.add("com.wwy.monitor.model.tddl.dto");
    }

    static Set<String> mapperLocations = new HashSet<String>();

    static {
        mapperLocations.add("classpath*:mapper/tddl/*.xml");
        mapperLocations.add("classpath*:mapper/*.xml");
        mapperLocations.add("classpath*:mapper/resource/*.xml");
        mapperLocations.add("classpath*:mapper/product/*.xml");
        mapperLocations.add("classpath*:mapper/mc/*.xml");
        mapperLocations.add("classpath*:mapper/version/*.xml");
        mapperLocations.add("classpath*:mapper/statistics/*.xml");
    }

    @Autowired
    private DataSource dataSource;


    @Bean(name = "sqlSessionFactory")
    public SqlSessionFactory sqlSessionFactoryBean() {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(dataSource);
        String aliasesPackage = packages.stream().reduce((s1, s2) -> s1 + "," + s2).get();
        bean.setTypeAliasesPackage(aliasesPackage);
        ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
        try {
            List<Resource> resources = new ArrayList<Resource>();
            for (String resourcePath : mapperLocations) {
                Resource[] res = resolver.getResources(resourcePath);
                if (res != null && res.length > 0) {
                    Collections.addAll(resources, res);
                }
            }
            //bean.setMapperLocations(resolver.getResources("classpath:mapper/*.xml"));
            bean.setMapperLocations(resources.toArray(new Resource[resources.size()]));
            return bean.getObject();
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }

    }

    @Bean
    public SqlSessionTemplate sqlSessionTemplate(SqlSessionFactory sqlSessionFactory) {
        return new SqlSessionTemplate(sqlSessionFactory);
    }

    @Bean
    @Override
    public PlatformTransactionManager annotationDrivenTransactionManager() {
        return new DataSourceTransactionManager(dataSource);
    }
}
